<template>
  <div class="container">
      <div class='login_content'>
        <div class='user_photo'><open-data type="userAvatarUrl"></open-data></div>
        <div class='user_nickname'><open-data type="userNickName"></open-data></div>
        <button  v-if="canIUse" class="login_btn" open-type="getUserInfo" @getuserinfo="bindGetUserInfo">点击授权登录</button>
        <div v-else>请升级微信版本</div>
        <button class="back_btn" @click="toHome">返回首页</button>
      </div>
  </div>
</template>
<script>
export default {
  data () {
    return {
      canIUse: wx.canIUse('button.open-type.getUserInfo'),
      loginType:''
    }
  },
  methods: {
     async bindGetUserInfo(e){
      var _this=this;
      uni.login({
          success : res =>{
             let code=res.code;
              uni.getUserInfo({
                success:async(res) =>{
					// let params={raw_data:res.rawData,signature:res.signature,code:code}
					let params={}
					
					params={raw_data:res.rawData,signature:res.signature,code:code};
					if(this.$bus.$data.inviteId){
						//邀请用户的id
						params.user_id=this.$bus.$data.inviteId
					}
					let res1=await this.$api.wxAppLogin(params)
                  if(res1.data.level=="success")
                    {
                      uni.setStorageSync('userToken',res1.data.data);

                      if(res1.data.action=='home')
                      { 
                        uni.showLoading({
                        title: '加载中',
                      })
                        // uni.switchTab({
                        //   url:"/pages/index/index"
                        // })
						_this.toLastPage()
                      }else{
                        uni.navigateTo({
                        	url:'/pages/login/register/register'
                        })
                      }
                    }else{
						uni.showToast({
							icon:'none',
							title:res1.data.message
						})
                    }
                
                }
                
              })
          }
      })

      
    },
    toHome(){
      uni.switchTab({
		  url:"/pages/index/index"
	  })
    },
	toLastPage(){
		//返回授权前的页面
		let pageArr=getCurrentPages();
		let index=0
		if(pageArr.length>2){
			index=pageArr.length-2
		}
		if(
		pageArr[index].route!=='pages/index/index'&&
		pageArr[index].route!== 'pages/circlebar/circlebar' &&
		pageArr[index].route!== 'pages/notice/notice' &&
		pageArr[index].route!== 'pages/person/person'
		){
			
			let options=null
			if(pageArr[index].options){
				options=this.$currency.toQueryString(pageArr[index].options)
			}
			uni.redirectTo({
				url: `/${pageArr[index].route}?${options}`
			});
		}else{
			uni.switchTab({
				url:`/${pageArr[index].route}`
			})
		}
		
	},
  },
  onLoad(options){
    // this.loginType=options.type
    // console.log(this.loginType)
	console.log(getCurrentPages())
  },
  // onHide(){
  //   this.loginType=''
  // }
}
</script>
<style scoped>
.login_content{width: 100%;position: absolute;left: 0;top: 20%;}
.user_photo{width: 100px;margin:0 auto;border-radius: 20px}
.user_nickname{text-align: center;margin-top: 20px}
button{
  width:150px;
  height: 50px;
  margin: 30px auto 10px auto;
  border-radius:10px;
  border: solid 1px green;
  background-color: green;
  color: #fff
  }
.back_btn{
  text-align: center;
  margin: 10px auto;
  border: solid 1px green;
  background-color: white;
  color: green
}

</style>
